import logging

from swagger_client import ApiClient, Configuration
from swagger_client.api import DemoControllerApi


class DemoClientApi(object):
    """
        1. 可以重写自动生成的请求方法, 使得书写case更加便捷
        2. 可以方法中增加log, 减少case中log代码的书写
        3. 自动生成的代码与case差异大，需要case兼容处理工作量大，可以在此处重写自动生成的代码以作适配
    """

    def __init__(self, host, port):
        demo_api_configuration = Configuration()
        demo_api_configuration.verify_ssl = False
        demo_api_configuration.host = "http://" + host + ":" + str(port)
        demo_api_client = ApiClient(
            configuration=demo_api_configuration,
        )
        self.http_api = DemoControllerApi(api_client=demo_api_client)

    def delete_demo(self, id):
        """删除demo"""
        logging.info(f"删除demo id: {id}")
        resp, status_code, _ = self.http_api.delete_demo_with_http_info(id=id)
        logging.info(f"删除demo resp: {resp}")

        if not hasattr(resp, "result") or status_code != 200:
            logging.error("获取demo列表报错, resp无result字段 或者 status_code不等于200")
            return False
        else:
            return resp

    def demo_list(self, **kwargs):
        """获取demo列表"""
        logging.info("获取demo列表kwargs: {}".format(kwargs))
        resp, status_code, _ = self.http_api.demo_list_with_http_info(
            **kwargs
        )
        logging.info("获取demo列表resp: {}".format(resp))
        if type(resp) is not list or status_code != 200:
            logging.error("获取demo列表报错, resp类型不是列表 或者 status_code不等于200")
            return None
        else:
            return resp

    def get_demo(self, id):
        """获取demo"""
        logging.info(f"获取demo id: {id}")
        resp, status_code, _ = self.http_api.get_demo_with_http_info(id=id)
        logging.info(f"获取demo resp: {resp}")

        if not hasattr(resp, "id") or status_code != 200:
            logging.error("获取demo报错, resp无id字段 或者 status_code不等于200")
            return None
        else:
            return resp

    def post_demo(self, name, type=None):
        """添加demo"""
        body = {
            "name": name
        }
        if type:
            body['type'] = type
        logging.info(f"添加demo body: {body}")
        resp, status_code, _ = self.http_api.post_demo_with_http_info(payload=body)
        logging.info(f"添加demo resp: {resp}")

        if not hasattr(resp, "id") or status_code != 200:
            logging.error("添加demo报错, resp无id字段 或者 status_code不等于200")
            return None
        else:
            return resp

    def put_demo(self, id, name, type=None):
        """修改demo"""
        body = {
            "name": name
        }
        if type:
            body['type'] = type
        logging.info(f"demo id: {id} 修改demo body: {body}")
        resp, status_code, _ = self.http_api.put_demo_with_http_info(id, payload=body)
        logging.info(f"修改demo resp: {resp}")

        if not hasattr(resp, "id") or status_code != 200:
            logging.error("修改demo报错, resp无id字段 或者 status_code不等于200")
            return None
        else:
            return resp
